package com.easy.tool.utils.ajax;

import com.easy.tool.anno.BeanDes;
import com.easy.tool.anno.MethodDes;
import com.easy.tool.enums.ResponseEnum;
import com.sun.jmx.snmp.EnumRowStatus;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.DecoderException;

/**
 * @author 高俊
 * @create 2021-09-2021/9/8-11:20
 */
@NoArgsConstructor
@BeanDes(name = "JsonResult2返回类型", des = "用于controller返回类型,相比于JsonResult更加简单，简洁，明了。")
@Data
public class JsonResult2<T> {

    private Integer code;

    private String codeName;

    private String msg;

    private T data;

    public JsonResult2(Integer code, String codeName, String msg, T data) {
        this.code = code;
        this.codeName = codeName;
        this.msg = msg;
        this.data = data;
    }
    public JsonResult2(Integer code, String codeName) {
        this.code = code;
        this.codeName = codeName;
    }
    public JsonResult2(String msg) {
        this.msg = msg;
    }


    /**
     * 请求成功
     * @return
     */
    @MethodDes(des = "请求成功",explain = "请求成功")
    public static JsonResult2 ok(){
        return new JsonResult2(ResponseEnum.OK.getCode(),ResponseEnum.OK.getCodeName());
    }

    /**
     * 请求成功
     * @param data
     * @param <T>
     * @return
     */
    @MethodDes(des = "请求成功",explain = "请求成功调用方法，添加默认的请求成功code和codeName")
    public static <T> JsonResult2 ok(T data){
        JsonResult2<T> result = new JsonResult2<>(ResponseEnum.OK.getCode(), ResponseEnum.OK.getCodeName());
        result.setData(data);
        return result;
    }

    /**
     * 请求失败
     * @param code
     * @param codeName
     * @return
     */
    @MethodDes(des = "请求失败",explain = "请求失败调用方法，自定翼code和codeName",param = {"arg0->code：请求响应码","arg1->codeName：请求响应码名称"})
    public static JsonResult2 faild(Integer code,String codeName){
        return new JsonResult2(code,codeName);
    }

    /**
     * 添加data数据
     * @param data
     * @return
     */
    @MethodDes(des = "添加data数据",explain = "添加data数据，通过ok或者faild方法调用之后，链式调用",param = {"arg0-> data：返回页面需要携带的数据"})
    public JsonResult2 data(T data){
        this.setData(data);
        return this;
    }

    /**
     * 添加msg消息
     * @param msg
     * @return
     */
    @MethodDes(des = "添加msg消息",explain = "添加msg消息",param = {"arg0->msg：返回页面需要自定义添加的消息信息"})
    public JsonResult2 msg(String msg){
        this.setMsg(msg);
        return this;
    }
    /*---------------------------------------------------*/

    /**
     * 自定义返回
     * @return
     */
    @MethodDes(des = "自定义返回",explain = "自定义返回",param = {"arg0->code：请求响应码","arg1->codeName：请求响应码名称"})
    public static JsonResult2 customResult(Integer code,String codeName){
        return new JsonResult2(code,codeName);
    }

    /**
     * 服务器错误
     * @return
     */
    @MethodDes(des = "服务器错误",explain = "系统默认服务器错误问响应码和名称")
    public static JsonResult2 serviceError(){
        return new JsonResult2(ResponseEnum.SERVICE_REFUSE.getCode(), ResponseEnum.SERVICE_ERROR.getCodeName());
    }

    /**
     * 无权限
     * @return
     */
    @MethodDes(des = "无权限",explain = "系统默认无权限响应码和名称")
    public static JsonResult2 noAuthen(){
        return new JsonResult2(ResponseEnum.NO_AUTH.getCode(), ResponseEnum.NO_AUTH.getCodeName());
    }

    /**
     * 请求超时
     * @return
     */
    @MethodDes(des = "请求超时",explain = "系统默认请求超时响应码和名称")
    public static JsonResult2 timeOut(){
        return new JsonResult2(ResponseEnum.TIME_OUT.getCode(), ResponseEnum.NO_AUTH.getCodeName());
    }

    /**
     * 服务器拒绝访问
     * @return
     */
    @MethodDes(des = "服务器拒绝访问",explain = "系统默认服务器拒绝访问响应码和名称")
    public static JsonResult2 serviceRefuse(){
        return new JsonResult2(ResponseEnum.SERVICE_REFUSE.getCode(), ResponseEnum.SERVICE_REFUSE.getCodeName());
    }

}
